const GroupBy = require("../template")

function testGroupByComplexScenarios() {
  console.log(
    "1. 数组分组复杂场景 - 嵌套对象分组:",
    (() => {
      const array = [
        { user: { profile: { level: 'beginner', score: 100 } } },
        { user: { profile: { level: 'intermediate', score: 200 } } },
        { user: { profile: { level: 'beginner', score: 150 } } },
        { user: { profile: { level: 'advanced', score: 300 } } }
      ]
      const result = GroupBy.groupBy(array, 'user.profile.level')
      return Object.keys(result).length === 3 && result['beginner'].length === 2
    })()
  )

  console.log(
    "2. 数组分组复杂场景 - 多条件组合分组:",
    (() => {
      const array = [
        { id: 1, category: 'A', status: 'active' },
        { id: 2, category: 'B', status: 'inactive' },
        { id: 3, category: 'A', status: 'active' },
        { id: 4, category: 'B', status: 'active' }
      ]
      const result = GroupBy.groupByMultiple(array, 'category', 'status')
      return Object.keys(result).length === 2
    })()
  )

  console.log(
    "3. 数组分组复杂场景 - 条件过滤分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithFilter(array, x => x % 2 === 0 ? 'even' : 'odd', x => x > 5)
      return Object.keys(result).length === 2 && result['even'].length === 3
    })()
  )

  console.log(
    "4. 数组分组复杂场景 - 映射转换分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithMap(array, x => x % 2 === 0 ? 'even' : 'odd', x => x * 2)
      return Object.keys(result).length === 2 && result['even'].length === 5
    })()
  )

  console.log(
    "5. 数组分组复杂场景 - 排序分组:",
    (() => {
      const array = [3, 1, 2, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithSort(array, x => x % 2 === 0 ? 'even' : 'odd', (a, b) => a - b)
      return Object.keys(result).length === 2 && result['odd'][0] === 1
    })()
  )

  console.log(
    "6. 数组分组复杂场景 - 统计分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithStats(array, x => x % 2 === 0 ? 'even' : 'odd')
      return Object.keys(result.result).length === 2 && typeof result.stats === 'object'
    })()
  )

  console.log(
    "7. 数组分组复杂场景 - 转换分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithTransform(array, x => x % 2 === 0 ? 'even' : 'odd', {
        even: arr => arr.map(x => x * 2),
        odd: arr => arr.map(x => x * 3)
      })
      return Object.keys(result).length === 2 && result['even'][0] === 4
    })()
  )

  console.log(
    "8. 数组分组复杂场景 - 验证分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupByWithValidation(array, x => x % 2 === 0 ? 'even' : 'odd', { min: 1, max: 10 })
      return Object.keys(result).length === 2 && result['even'].length === 5
    })()
  )

  console.log(
    "9. 数组分组复杂场景 - 混合类型分组:",
    (() => {
      const array = [1, '1', 2, '2', 3, '3', true, false, null, undefined]
      const result = GroupBy.groupBy(array, x => typeof x)
      return Object.keys(result).length === 4 && result['number'].length === 3
    })()
  )

  console.log(
    "10. 数组分组复杂场景 - 函数分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const result = GroupBy.groupBy(array, x => {
        if (x <= 3) return 'small'
        if (x <= 6) return 'medium'
        return 'large'
      })
      return Object.keys(result).length === 3 && result['small'].length === 3
    })()
  )

  console.log(
    "11. 数组分组复杂场景 - 异步分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      return GroupBy.groupByAsync(array, x => x % 2 === 0 ? 'even' : 'odd', async x => x * 2).then(result => {
        return Object.keys(result).length === 2 && result['even'].length === 5
      })
    })()
  )

  console.log(
    "12. 数组分组复杂场景 - 并发分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      return GroupBy.groupByConcurrent(array, x => x % 2 === 0 ? 'even' : 'odd', async x => x * 2, 3).then(result => {
        return Object.keys(result).length === 2 && result['even'].length === 5
      })
    })()
  )

  console.log(
    "13. 数组分组复杂场景 - 可取消分组:",
    (() => {
      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      const controller = { cancelled: false }
      const result = GroupBy.groupByWithCancel(array, x => x % 2 === 0 ? 'even' : 'odd', controller)
      return Object.keys(result).length === 2 && result['even'].length === 5
    })()
  )

  console.log(
    "14. 数组分组复杂场景 - 边界条件:",
    (() => {
      const array = [null, undefined, null, undefined]
      const result = GroupBy.groupBy(array, x => x)
      return Object.keys(result).length === 2 // null和undefined应该被区分
    })()
  )

  console.log(
    "15. 数组分组复杂场景 - 特殊值处理:",
    (() => {
      const array = [NaN, NaN, Infinity, -Infinity, Infinity]
      const result = GroupBy.groupBy(array, x => x)
      return Object.keys(result).length === 3 // NaN, Infinity, -Infinity
    })()
  )

  console.log(
    "16. 数组分组复杂场景 - 大数处理:",
    (() => {
      const array = [
        Number.MAX_SAFE_INTEGER,
        Number.MAX_SAFE_INTEGER,
        Number.MIN_SAFE_INTEGER,
        Number.MIN_SAFE_INTEGER
      ]
      const result = GroupBy.groupBy(array, x => x)
      return Object.keys(result).length === 2 // 两种不同的大数
    })()
  )

  console.log(
    "17. 数组分组复杂场景 - 性能边界:",
    (() => {
      const largeArray = Array.from({ length: 100000 }, (_, i) => ({ id: i % 1000, value: i }))
      const start = performance.now()
      const result = GroupBy.groupBy(largeArray, 'id')
      const end = performance.now()
      return (end - start) < 1000 && Object.keys(result).length === 1000 // 10万条数据应该在1秒内完成
    })()
  )

  console.log(
    "18. 数组分组复杂场景 - 内存压力:",
    (() => {
      const largeArray = Array.from({ length: 50000 }, (_, i) => ({
        id: i % 1000,
        data: { value: i, nested: { deep: i % 10 } }
      }))
      const start = performance.now()
      const result = GroupBy.groupBy(largeArray, 'id')
      const end = performance.now()
      return (end - start) < 500 && Object.keys(result).length === 1000
    })()
  )
}

testGroupByComplexScenarios()
